package ru.housewife.service.wares;

import org.hibernate.Query;
import org.hibernate.Session;
import ru.common.hibernate.HibernateAction;
import ru.housewife.form.IngredientGroupForm;
import ru.housewife.model.IngredientGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO: add description
 *
 * @author atroshanin
 */
public class GetIngredientsGroupsTree implements HibernateAction<List<IngredientGroupForm>> {

    @Override
    public List<IngredientGroupForm> execute(Session session) {
        Query q = session.createQuery("select ig from IngredientGroup ig where ig.parent.id is NULL");
        List<IngredientGroup> lst = q.list();
        List<IngredientGroupForm> result = new ArrayList<IngredientGroupForm>(lst.size());
        for (IngredientGroup ig : lst) {
            IngredientGroupForm frm = new IngredientGroupForm(ig);
            result.add(frm);
        }
        return result;
    }

}
